package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.system.domain.TeaShopOrder;
import com.ruoyi.system.service.ITeaShopOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Expend;
import com.ruoyi.system.service.IExpendService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 支出信息Controller
 * 
 * @author ruoyi
 * @date 2024-05-16
 */
@Controller
@RequestMapping("/system/expend")
public class ExpendController extends BaseController
{
    private String prefix = "system/expend";

    @Autowired
    private IExpendService expendService;

    @Autowired
    private ITeaShopOrderService teaShopOrderService;

    @RequiresPermissions("system:expend:view")
    @GetMapping()
    public String expend()
    {
        return prefix + "/expend";
    }

    @GetMapping("/analyze")
    public String analyze(ModelMap mmap)
    {
        List<Expend> expends = expendService.selectExpendList(null);
        long salaryCost = 0;
        long importCost = 0;
        for (Expend expend : expends) {
            if (expend.getType().equals("1"))
                salaryCost += expend.getCost();
            else
                importCost += expend.getCost();
        }
        mmap.put("salaryCost", salaryCost);
        mmap.put("importCost", importCost);
        mmap.put("cost", salaryCost + importCost);

        List<TeaShopOrder> orders = teaShopOrderService.selectTeaShopOrderList(null);
        String[] commodity = {"红茶", "绿茶", "桔茶", "香蕉茶", "椰子茶", "柠檬茶", "三明治"};
        int[] amounts = {0, 0, 0, 0, 0, 0, 0};
        for (TeaShopOrder order : orders) {
            String[] split = order.getContent().split(",");
            for (int i = 0; i < split.length; i++) {
                String[] temp = split[i].split("\\*");
                for (int j = 0; j < commodity.length; j++) {
                    if (temp[0].equals(commodity[j])) {
                        amounts[j] += Integer.parseInt(temp[1]);
                    }
                }
            }
        }
        long income = 0;
        for (int i = 0; i < amounts.length; i++) {
            income += amounts[i];
        }
        mmap.put("a", amounts[0]);
        mmap.put("b", amounts[1]);
        mmap.put("c", amounts[2]);
        mmap.put("d", amounts[3]);
        mmap.put("e", amounts[4]);
        mmap.put("f", amounts[5]);
        mmap.put("g", amounts[6]);
        mmap.put("income", income);
        return prefix + "/analyze";
    }
    /**
     * 查询支出信息列表
     */
    @RequiresPermissions("system:expend:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Expend expend)
    {
        startPage();
        List<Expend> list = expendService.selectExpendList(expend);
        return getDataTable(list);
    }

    /**
     * 导出支出信息列表
     */
    @RequiresPermissions("system:expend:export")
    @Log(title = "支出信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Expend expend)
    {
        List<Expend> list = expendService.selectExpendList(expend);
        ExcelUtil<Expend> util = new ExcelUtil<Expend>(Expend.class);
        return util.exportExcel(list, "支出信息数据");
    }

    /**
     * 新增支出信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存支出信息
     */
    @RequiresPermissions("system:expend:add")
    @Log(title = "支出信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Expend expend)
    {
        return toAjax(expendService.insertExpend(expend));
    }

    /**
     * 修改支出信息
     */
    @RequiresPermissions("system:expend:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Expend expend = expendService.selectExpendById(id);
        mmap.put("expend", expend);
        return prefix + "/edit";
    }

    /**
     * 修改保存支出信息
     */
    @RequiresPermissions("system:expend:edit")
    @Log(title = "支出信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Expend expend)
    {
        return toAjax(expendService.updateExpend(expend));
    }

    /**
     * 删除支出信息
     */
    @RequiresPermissions("system:expend:remove")
    @Log(title = "支出信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(expendService.deleteExpendByIds(ids));
    }
}
